/*
 * @Author: BUG yangcheng960813@163.com
 * @Date: 2024-05-17 10:15:56
 * @LastEditTime: 2024-07-12 15:49:43
 * @LastEditors: yayalee yangcheng960813@163.com
 * @Description: 
 * @FilePath: \unfinished-challenges\assets\core_ts\StringUtil.ts
 * 可以输入预定的版权声明、个性签名、空行等
 */
import { MathUtil } from "./MathUtil";
import { isEmpty, isNumber, isString } from "./TsUtil";

export class StringUtil {

    /**
     * @description: 字符串填充
     * @param str 字符串
     * @param len 填充长度
     * @param content 填充内容
     * @param startOrEnd padStart or padEnd defaule == true padStart
     * @return {*}
     * @author: yayalee
     * @since: v1.0.0
     * ```
     * padStr("1",2,"0");
     * //=> "01"
     * padStr("1",2,"0",false);
     * //=> "10"
     * ```
     */
    public static padStr(str: string, len: number, content?: string, startOrEnd?: boolean): string
    public static padStr(str: any, len: number, content?: string, startOrEnd?: boolean): string
    public static padStr(str: string, len: number, content?: string, startOrEnd = true): string {
        if (!isString(str)) str = str.toString();
        //es2017
        //@ts-ignore 
        if (String.prototype.hasOwnProperty("padStart") && String.prototype.hasOwnProperty("padEnd"))
            //@ts-ignore 
            return startOrEnd ? str.padStart(len, content) : str.padEnd(len, content);
        //兼容代码
        //floor if number or convert non-number to 0;
        //floor if number 或将 non-number 转换为0;
        len = len >> 0;
        content = String(typeof content !== 'undefined' ? content : ' ');
        if (str.length > len) {
            return str;
        } else {
            len = len - str.length;
            if (len > content.length) {
                //append to original to ensure we are longer than needed
                //附加到原文，以确保我们比需要的更长
                content += content.repeat(len / content.length);
            }
            if (startOrEnd) {
                return content.slice(0, len) + str;
            } else {
                return str + content.slice(0, len);
            }
        }
    }

    /**
     * @description: 根据参数返回格式化字符串
     * @param {string} text 源字符串
     * @param {array} option 用于格式化源字符串的数据，可以是键值对，也可以按顺序传参
     * @return {*}
     * @author: yayalee
     * @since: v1.0.0
     * ```
     * //可使用以下两种调用方式，返回结果都是"测试字符串111--abc..."
     * _js.formatString("测试字符串%{a1}--%{a2}...", {a1: 111, a2: "abc"});
     * _js.formatString("测试字符串%{a1}--%{a2}...", 111, "abc");
     * ```
     */
    public static formatString(text: string, ...option: [Record<string, string | number>] | Array<string | number | any>): string {
        let result = text;
        if (isEmpty(result)) return result;
        if (option.length === 1 && Object.prototype.toString.call(option[0]) === "[object Object]") {
            // 参数为键值对
            for (let arg in (option[0] as Record<string, string | number>)) {
                if (option[0].hasOwnProperty(arg)) {
                    let reg = new RegExp(`%{${arg}}`, "g");
                    result = result.replace(reg, `${option[0][arg]}`);
                }
            }
        } else {
            // 参数为数组
            option.forEach((value: any) => {
                result = result.replace(/%\{.*?\}/, `${value}`);
            });
        }
        return result;
    }

    public static toFixedWan(value: number): string {
        const wanFix = 100000;
        let wanFloat = wanFix / 10;
        var v = Math.floor(value / wanFloat) * wanFloat;
        return value > wanFix ? `${MathUtil.toFixedStr(v / wanFix * 10, 1)}万` : value.toString();
    }

    /**
     * @description: 
     * @param str 字符串
     * @param start 开始位置
     * @param threshold 界限
     * @param suffix 后缀
     * @return {*}
     * @author: yayalee
     * @since: v1.0.0
     */
    public static clampStr(str: number | string, start: number, threshold: number, suffix: string): string;
    public static clampStr(str: string, start: number, threshold: number, suffix = '...'): string {
        if (isNumber(str)) str = str.toString();
        if (str.replace(/[^\x00-\xff]/g, 'xx').length <= threshold) return str;
        let charCount = 0;
        let result = '';
        for (let i = start; i < str.length; i++) {
            charCount += /[^\x00-\xff]/.test(str[i]) ? 2 : 1;
            if (charCount > threshold) return result += suffix;
            result += str[i];
        }
        return result;
    }

}